const express = require('express')

const ActiveRouter = express.Router()
const { adminLists } = require('../shema/adminUserSchema')
const getToken = require('../model/qiniuConfig')


/* 路由首位 */
/* 注册 */
ActiveRouter.post('/register', async(req, res) => {
    let { nick_name, password } = req.body
    let searchStatus = await adminLists.findOne({ nick_name })

    if (searchStatus) {
        res.send({ code: 1, errInfo: '用户名已经注册' })
    } else {
        let insertStatus = await adminLists.create(req.body)
        req.session.userInfo = { nick_name: insertStatus.nick_name, avatar: insertStatus.avatar }
        req.session.registerStatus = 1
        res.send({ code: 0, msg: '注册成功' })
    }
})

// 登陆
ActiveRouter.post('/login', async(req, res) => {
    let { nick_name, password } = req.body

    let searchStatus = await adminLists.findOne({ nick_name })

    if (searchStatus) {
        if (searchStatus.password == password) {
            req.session.registerStatus = 1
            res.send({ code: 0, msg: '登陆成功', avatar: searchStatus.avatar })
        } else {
            res.send({ code: 1, errorInfo: '密码错误' })
        }
    } else {
        res.send({ code: 1, errorInfo: '用户名错误' })
    }
})

ActiveRouter.get('/getToken', (req, res) => {
    let token = getToken({
        url: 'http://pufa5zpxi.bkt.clouddn.com',
        scope: 'ruohan_admin_avatar'
    })
    res.send({ code: 0, msg: token })
})


module.exports = ActiveRouter